home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STACKS.PAK / STACK.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  1KB  |  56 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // stack.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/stacks.h>
  8. #include <iostream.h>
  9. #include <strstrea.h>
  10. #include "../myclass.h" 
  11.  
  12. typedef TStackAsVector<MyClass> ContainerType;
  13. typedef TStackAsVectorIterator<MyClass> IteratorType;
  14.  
  15. const int MaxItems=6;
  16.  
  17. void ForEachCallBack(MyClass& mc, void* s)
  18. {
  19.     cout << (char*)s << mc << endl;
  20. }
  21.  
  22. void AddItems(ContainerType& container, int numItems)
  23. {
  24.     for( int i=numItems; i>0; i-- )
  25.         {
  26.         char buf[80];
  27.         ostrstream str( buf, sizeof(buf) );
  28.         str << i << " hello" << ends;
  29.         container.Push(MyClass(buf));
  30.         }
  31. }
  32.  
  33. void UseForwardIterator(ContainerType& container)
  34. {
  35.     IteratorType iterator(container);
  36.     while( iterator )
  37.         {
  38.         cout << iterator.Current() << endl;
  39.         iterator++;
  40.         }
  41. }
  42.  
  43. int main()
  44. {
  45.     ContainerType container;
  46.     AddItems(container, MaxItems);
  47.   
  48.     cout << "--- Starting ForEach" << endl;
  49.     container.ForEach(ForEachCallBack, (void*)"FE ");
  50.   
  51.     cout << "--- Starting Iterator" << endl;
  52.     UseForwardIterator(container);
  53.   
  54.     return 0;
  55. }
  56.